package com.viknix.threadpool.manager.server.dependence.factory;

import com.viknix.threadpool.manager.common.constant.Constants;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: Dongqi
 * @Date: 2021/11/11 17:47
 * @Version 1.0
 * @Description: 名称线程工厂（守护线程）
 */
public class NameThreadFactory implements ThreadFactory {

    private final AtomicInteger id = new AtomicInteger(0);

    private String name;

    public NameThreadFactory(String name) {
        if (!name.endsWith(Constants.DOT)) {
            name += Constants.DOT;
        }
        this.name = name;
    }

    @Override
    public Thread newThread(Runnable r) {
        String threadName = name + id.getAndIncrement();
        Thread thread = new Thread(r, threadName);
        thread.setDaemon(true);
        return thread;
    }
}
